--  Creates testdata.
--
--  Copyright (C) 2017 Markus Zeindl
--
--  This file is part of pg_copyplot, an adapter for plotting tools 
--  employing postgresql's COPY command.
--
--  This program is free software: you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation, either version 3 of the License, or
--  (at your option) any later version.
--
--  This program is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program.  If not, see <http://www.gnu.org/licenses/>.

DROP TABLE IF EXISTS t_test;
CREATE TABLE t_test (
       id SERIAL PRIMARY KEY,
       factor_var VARCHAR(512),
       var_value FLOAT
);

INSERT INTO t_test (factor_var, var_value)
(SELECT 'apple', c+random()*-1^(round(random()))
 FROM generate_series(-100,100) t, (VALUES(2)) l(c)
);

INSERT INTO t_test (factor_var, var_value)
(SELECT 'banana', b*t+c+random()*-1^(round(random()))
 FROM generate_series(-10,10) t, (VALUES(2)) l(c), (VALUES(0.7)) m(b)
);

INSERT INTO t_test (factor_var, var_value)
(SELECT 'pineapple', b*t^2+c+random()*-1^(round(random()))
 FROM generate_series(-10,10) t, (VALUES(2)) l(c), (VALUES(0.25)) m(b)
);

INSERT INTO t_test (factor_var, var_value)
(SELECT 'cherry', b*t^3+c+random()*-1^(round(random()))
 FROM generate_series(-5,5) t, (VALUES(2)) l(c), (VALUES(0.00125)) m(b)
);


COPY t_test TO PROGRAM 'sed s/,/\\t/g | cat > /tmp/testdata.csv'
WITH (FORMAT csv, HEADER true);

DROP TABLE t_test;
